document.addEventListener('DOMContentLoaded', function () {


    function init() {
        chrome.storage.local.get(['isAutoSignIn', 'executionTime', 'entriesMax', 'todaySearchPoints'], function (results) {

            //词条上限数量
            if (results.entriesMax) {
                document.getElementById("entriesMax").value = results.entriesMax;
            }

            //自动签到
            if (results.isAutoSignIn) {
                console.log("自动签到已" + (results.isAutoSignIn === "1" ? "开启" : "关闭"))
                document.getElementById("isAutoSignIn").checked = results.isAutoSignIn === "1";
            }

            //执行时间
            if (results.executionTime) {
                document.getElementById("executionTime").value = results.executionTime;
            }

            if (results.todaySearchPoints) {
                document.getElementById("state").innerHTML = results.todaySearchPoints;
            }

        })
    }

    init()

    document.getElementById("isAutoSignIn").addEventListener("change", function (ev) {
        chrome.storage.local.set({'isAutoSignIn': ev.target.checked ? "1" : "0"}, function () {
            console.log("自动签到已" + (ev.target.checked ? "开启" : "关闭"));
        })
    })

    document.getElementById("executionTime").addEventListener("change", function (ev) {
        chrome.storage.local.set({'executionTime': ev.target.value}, function () {
            console.log("执行时间已修改为：" + ev.target.value);
        })
    })

    document.getElementById("entriesMax").addEventListener("change", function (ev) {
        chrome.storage.local.set({'entriesMax': ev.target.value}, function () {
            console.log("词条上限数量已修改为：" + ev.target.value);
        })
    })

    document.getElementById("btnSearch").addEventListener("click", function (ev) {
        chrome.storage.local.set({'manualSearch': "1"}, function () {
            window.open('https://www.toutiao.com');
        })
    })

    document.getElementById("btnClear").addEventListener("click", function (ev) {
        chrome.storage.local.remove(['entry', 'progress', 'toutiaoDate', 'searchDate', 'manualSearch', 'limitedDate', 'todaySearchPoints', 'isLock'])
    })
});
